<?php

namespace App\Http\Controllers;

use App\Models\Buy;
use App\Models\Sell;
use Illuminate\Http\Request;

class StatisController extends Controller
{
  public function dashboardForAdmin()
  {
    $statis = cache()->remember('dashboard-admin', 1, function () {
      return [
        'solds_count_today' => Sell::state('done')->today()->count(),
        'solds_coins_sum_today' => Sell::state('done')->today()->sum('coins'),
        'buys_count_today' => Buy::today()->count(),
        'buys_coins_sum_today' => Buy::today()->sum('coins'),
        'buys_done_count_today' => Buy::done()->today()->count(),
        'buys_done_coins_sum_today' => Buy::done()->today()->sum('coins'),
        'buys_seller_fee_sum_today' =>  Buy::today()->sum('seller_fee'),
        'buys_seller_tip_sum_today' =>  Buy::today()->sum('seller_tip'),
        'boughts_agent_fee_sum_today' =>  Buy::state('unfreeze')->today()->sum('agent_fee'),
        'boughts_agent_tip_sum_today' =>  Buy::state('unfreeze')->today()->sum('agent_tip'),
        'buys_fee_sum_today' => Buy::sum('fee'),
        'buys_tip_sum_today' => Buy::sum('tip'),
        // 'solds_count_today' => Sell::state('done')->today()->count(),
        // 'agencies_fee_sum' => Agency::sum('coins')
      ];
    });

    return success($statis);
  }

  public function dashboardForSeller()
  {
    $statis = cache()->remember('dashboard-seller-'  . auth()->id(), 300, function () {

      $buys = Buy::where('seller_id', sellerId())
        // ->where('state', 'unfreeze')
        ->today();

      $sells = Sell::where('seller_id', sellerId())
        // ->where('state', 'done')
        ->today();

      return [
        'buys_count_today' => $buys->count(),
        'buys_coins_sum_today' => (float) $buys->sum('coins'),
        'buys_fee_sum_today' => (float) $buys->sum('seller_fee'),
        'buys_tip_sum_today' => (float) $buys->sum('seller_tip'),
        'sells_count_today' => $sells->count(),
        'sells_coins_sum_today' => (float) $sells->sum('coins'),
      ];
    });
    return success($statis);
  }

  public function dashboardForAgent()
  {
    $statis = cache()->remember('dashboard-agent-' . auth()->id(), 300, function () {
      $query1 = Buy::where('agent_id', auth()->id())->state('unfreeze')->today();
      $query2 = clone $query1;
      // $query3 = clone $query1;

      $agent = auth()->user();
      return [
        'buys_count_today' => $query1->count(),
        'buys_sum_tip_fee' => $query2->sum('agent_tip_fee'),
        'coins' => $agent->coins,
        'fee_ratio' => $agent->fee_ratio,
        'tip' => $agent->tip,
        'cut' => $agent->cut
      ];
    });
    return success($statis);
  }

  public function noticeCount()
  {
    $arr = [
      'taking_count' => takingCount(),
      'checking_count' => sellingCount()
    ];
    return success($arr);
  }

  public function tiemoutCount()
  {
    $limit = now()->subMinutes(sys('buy_waiting_max_minutes'));
    $count = Buy::latest()
      ->where('state', 'waiting')
      ->where('created_at', '<', $limit)
      ->count();
    return success($count);
  }
}
